//
//  ZCSeeResultVC.m
//  zctx
//
//  Created by apple on 15/10/8.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCSamplingVC.h"
#import "ZCInvolvedDetailsVC.h"
#import "ZCInvolvedModel.h"
#import "ZCSearchQualifiedCell.h"
#import "ZCSamplingOKCell.h"

#import "ZCmainCell.h"
#import "ZCSamplingCell.h"
@interface ZCSamplingVC ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *dataArray;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,assign)NSInteger offset;
@end

@implementation ZCSamplingVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCSamplingVC"];
    [MTA trackPageViewBegin:@"ZCSamplingVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCSamplingVC"];
    [MTA trackPageViewEnd:@"ZCSamplingVC"];
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
     [self requestSeeResultdata];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    self.navigationItem.title = @"最新报告";
    dataArray = [NSMutableArray arrayWithCapacity:0];
    [self initnavegation];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    _offset = 0;
    [self initTableView];
    [self requestSeeResultdata]; //请求数据

    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSearchQualifiedCell" bundle:nil] forCellReuseIdentifier:@"SearchQualified"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSamplingCell" bundle:nil] forCellReuseIdentifier:@"samcell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSamplingOKCell" bundle:nil] forCellReuseIdentifier:@"samOKcell"];
    
    self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

#pragma mark 导航栏
-(void)initnavegation{

    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 加载更多
-(void)loadMoreData{
    //上拉加载
    _offset = _offset+1;
    [self requestSeeResultdata];
    
}

#pragma mark 请求抽样数据
-(void)requestSeeResultdata{
    if (_offset==0) {
        NSLog(@"sdf");
        [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    }
    [NetworkManager requestFoundingGetAllWithType:@"3" mid:UserModel.mid password:UserModel.password mtype:@"0" pagecount:[NSString stringWithFormat:@"%ld",(long)_offset] keywords:@"" success:^(NSDictionary *array) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
         [self.tableView.mj_footer endRefreshing];

        if ([[array objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            NSArray *sarray = (NSArray *)[array objectForKey:@"content"];
            if (sarray.count>0) {
                for (NSDictionary *dict in sarray) {
                    [dataArray addObject:dict];
                }
                [self.tableView reloadData];
            }
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        self.bannerView.hidden = NO;
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.separatorColor = RGB(236, 237, 238);
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCInvolvedModel *moel =[ZCInvolvedModel modelObjectWithDictionary:dataArray[indexPath.row]];
    if ([moel.result isEqualToString:@"2"]) {
        static NSString *cellid =@"samcell";
        ZCSamplingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[ZCSamplingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        cell.samplingModel = moel;
        return cell;

    }else{
        static NSString *cellid =@"samOKcell";
        ZCSamplingOKCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[ZCSamplingOKCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        cell.samplingOKModel = moel;
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 92;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *mid =[NSString stringWithFormat:@"%@",[dataArray[indexPath.row] objectForKey:@"id"]];
    NSString *testing_result =[NSString stringWithFormat:@"%@",[dataArray[indexPath.row] objectForKey:@"testing_result"]];
    ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
    detailVC.pid = mid;
    detailVC.stateInt = 3;
    detailVC.resultStr = testing_result;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
